package com.example.softwarearchitectureexamples.service;

import com.example.softwarearchitectureexamples.dto.Address;
import com.example.softwarearchitectureexamples.dto.User;
import com.example.softwarearchitectureexamples.repository.AddressRepository;
import com.example.softwarearchitectureexamples.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//业务逻辑层
@Service
//@RequiredArgsConstructor
public class UserService {

    //依赖注入
    private final UserRepository userRepository;
    private final AddressRepository addressRepository;

    public UserService(UserRepository userRepository, AddressRepository addressRepository) {
        this.userRepository = userRepository;
        this.addressRepository = addressRepository;
    }

    //新增用户
    @Transactional
    public User addUser(User user) {
        return userRepository.save(user);
    }

    //新增地址
    @Transactional
    public Address addAddress(Address address) {
        return addressRepository.save(address);
    }

    //通过账号密码获取用户全部信息
    public User getUser(String account, String password) {
        return userRepository.find(account, password);
    }

    //通过用户id获取用户全部地址
    public List<Address> listAddresses (int uid) {
        return addressRepository.findAddresses(uid);
    }

    //通过用户id获取用户全部信息
    public User getUser(int uid) {
        return userRepository.findById(uid).orElse(null);
    }
}